<?php

namespace App\Http\Controllers\Internal;

use App\Models\Course;

class CourseController extends Controller
{
    /*
     * @api get /internal/courses 获取课程列表
     * @version 0.0.1
     * @group internal
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "total":30,
     *    "items":[
     *      "id":"0",
     *      "star":"",
     *      "title":"",
     *      "cover":"",
     *      "date":"",
     *      "day":""
     *    ]
     *  }
     * }
     *
     */
    public function index()
    {
        $this->validate($this->request, [
            'courseIds' => 'array'
        ]);
        $courseIds = $this->request->input('courseIds');
        $items = [];
        if ($courseIds) {
            $coursesObj = Course::find($courseIds);
            /** @var Course $courseObj */
            foreach ($coursesObj as $courseObj) {
                $cover = $courseObj->cover()->value('uri');
                if ($cover) {
                    $cover .= '?x-oss-process=style/lesson_thumb_cover';
                }
                $items[$courseObj->getId()] = [
                    'id' => $courseObj->getId(),
                    'price' => $courseObj->getPrice(),
                    'level' => $courseObj->getLevel(),
                    'cover' => $cover,
                    'discountPrice' => $courseObj->getDiscountPrice()
                ];
            }
        } else {
            $coursesObj = Course::hasPublished()->get();
            /** @var Course $courseObj */
            foreach ($coursesObj as $courseObj) {
                $cover = $courseObj->cover()->value('uri');
                if ($cover) {
                    $cover .= '?x-oss-process=style/lesson_thumb_cover';
                }
                $items[] = [
                    'id' => $courseObj->getId(),
                    'name' => $courseObj->getName(),
                    'description' => $courseObj->getDescription(),
                    'level' => $courseObj->getLevel(),
                    'lessonCount' => $courseObj->getLessonCount(),
                    'cover' => $cover,
                ];
            }
        }
        return $this->sendSuccessJson($items);
    }

    /*
     * @api get /internal/courses/{courseId} 获取课程
     * @version 0.0.1
     * @group internal
     *
     * @param path int courseId 课程ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     *
     */
    public function show($courseId)
    {
        $data = [];
        /** @var Course $courseObj */
        $courseObj = Course::find($courseId);
        if ($courseObj) {
            $cover = $courseObj->cover()->value('uri');
            if ($cover) {
                $cover .= '?x-oss-process=style/lesson_thumb_cover';
            }
            $data = [
                'id' => $courseObj->getId(),
                'name' => $courseObj->getName(),
                'level' => $courseObj->getLevel(),
                'lessonCount' => $courseObj->getLessonCount(),
                'cover' => $cover,
                'description' => $courseObj->getDescription(),
                'seriesName' => $courseObj->series()->value('name'),
            ];
        }
        return $this->sendSuccessJson($data);
    }


    /*
     * @api get /internal/courses/level/{level} 获取课程
     * @version 0.0.1
     * @group internal
     *
     * @param path int courseId 课程ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{}
     * }
     *
     */
    public function getByLevel($level)
    {
        $data = [];
        /** @var Course $courseObj */
        $courseObj = Course::where('level', $level)->first();
        if ($courseObj) {
            $data = [
                'id' => $courseObj->getId(),
            ];
        }
        return $this->sendSuccessJson($data);
    }

}
